﻿using Cloud.Core.Model;
using Cloud.Core.Response;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cloud.Core.Bizlogic;

namespace Cloud.Core.AuthUser
{
    public class Online
    {
        public static ConcurrentDictionary<string, Character> UserOnlineList = new ConcurrentDictionary<string, Character>();

        public static string LogIn(User UserInfo)
        {
            lock (UserOnlineList)
            {
                var guid = Guid.NewGuid().ToString();
                var character = new Character();

                character.IsLogin = true;
                //character.UserName = UserInfo.UserName;
                //character.PassWord = UserInfo.UserPwd;

                character.StartTime = DateTime.Now;
                character.EndTime = character.StartTime.AddSeconds(7200);
                character.PowerValue.SetPower(UserInfo);

                if (UserOnlineList.ContainsKey(guid))
                {
                    return "";
                }
                else
                {
                    UserOnlineList.TryAdd(guid, character);
                    List<string> dirtyData = new List<string>();
                    foreach (var info in UserOnlineList)
                    {
                        if (DateTime.Now - info.Value.StartTime > TimeSpan.FromHours(24))
                        {
                            dirtyData.Add(info.Key);
                        }
                    }
                    foreach (var key in dirtyData)
                    {
                        UserOnlineList.TryRemove(key, out character);
                    }
                    return guid;
                }
            }
        }

        public static bool LogOut(string Token)
        {
            if (UserOnlineList.ContainsKey(Token))
            {
                UserOnlineList.TryRemove(Token, out Character character);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Character GetLoginInfo(string Token)
        {
            Character character = new Character();
            Token = (Token != null) ? Token.Replace("Bearer ","") : "";
            if (Token == "admin123456")
            {
                character = new Character
                {
                    UserName = "TestUser",
                    IsLogin = true,
                    PowerValue = new Character.PowerList(),
                };
                character.PowerValue.MBasicInfo.SetValue(true);
                character.PowerValue.MContract.SetValue(true);
                character.PowerValue.MDataTable.SetValue(true);
                character.PowerValue.MGuoBang.SetValue(true);
                character.PowerValue.MLog.SetValue(true);
                character.PowerValue.MPlan.SetValue(true);
                character.PowerValue.MSysParam.SetValue(true);
                character.PowerValue.MUser.SetValue(true);
                return character;
            }
            return UserOnlineList.ContainsKey(Token) ? UserOnlineList[Token] : character;
        }

        public static JsonResponse AuthUser(ModOperate.Mod Model, ModOperate.Operate Operate, Character Characters)
        {
            if (Characters.IsLogin)
            {
                Power mPower = new Power();

                string errMsg = "";
                ResponseStatus status = ResponseStatus.Success;
                switch (Model)
                {
                    case ModOperate.Mod.MBasicInfo:
                        mPower = Characters.PowerValue.MBasicInfo;
                        break;
                    case ModOperate.Mod.MContract:
                        mPower = Characters.PowerValue.MContract;
                        break;
                    case ModOperate.Mod.MDataTable:
                        mPower = Characters.PowerValue.MDataTable;
                        break;
                    case ModOperate.Mod.MGuoBang:
                        mPower = Characters.PowerValue.MGuoBang;
                        break;
                    case ModOperate.Mod.MLog:
                        mPower = Characters.PowerValue.MLog;
                        break;
                    case ModOperate.Mod.MPlan:
                        mPower = Characters.PowerValue.MPlan;
                        break;
                    case ModOperate.Mod.MSysParam:
                        mPower = Characters.PowerValue.MSysParam;
                        break;
                    case ModOperate.Mod.MUser:
                        mPower = Characters.PowerValue.MUser;
                        break;
                }
                switch (Operate)
                {
                    case ModOperate.Operate.Add:
                        if (!mPower.Add) { errMsg = "无添加权限"; status = ResponseStatus.PowerError; }
                        break;
                    case ModOperate.Operate.Delete:
                        if (!mPower.Delete) { errMsg = "无删除权限"; status = ResponseStatus.PowerError; }
                        break;
                    case ModOperate.Operate.Edit:
                        if (!mPower.Edit) { errMsg = "无修改权限"; status = ResponseStatus.PowerError; }
                        break;
                    case ModOperate.Operate.Output:
                        if (!mPower.OutPut) { errMsg = "无导出权限"; status = ResponseStatus.PowerError; }
                        break;
                    case ModOperate.Operate.Print:
                        if (!mPower.Print) { errMsg = "无打印权限"; status = ResponseStatus.PowerError; }
                        break;
                    case ModOperate.Operate.Search:
                        if (!mPower.Search) { errMsg = "无查看权限"; status = ResponseStatus.PowerError; }
                        break;
                }
                return new JsonResponse()
                {
                    Msg = errMsg,
                    Status = status,
                };
            }
            else
            {
                return new JsonResponse
                {
                    Msg = "用户未登录",
                    Status = ResponseStatus.AuthFail,
                };
            }
        }

        public static JsonResponse<T> AuthUser<T>(ModOperate.Mod Model, ModOperate.Operate Operate, Character Characters)
        {
            var res = AuthUser(Model, Operate, Characters);
            return new JsonResponse<T>
            {
                Msg = res.Msg,
                Status = res.Status,
            };
        }
    }
}
